﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using Microsoft.Win32;
using Microsoft.Deployment.WindowsInstaller;

namespace TPG.SharePoint.Workflow.Install.VisualStudio
{
    public class VisualStudioCustomActions
    {
        [CustomAction]
        public static ActionResult InstallVSTemplates(Session session)
        {
            string vsPath = session.CustomActionData.ToString();
            session.Log("InstallVSTemplates: Started using path '{0}'", vsPath);            

            Record record = new Record(3);
            record[1] = "callAddProgressInfo";
            record[2] = "Installing Visual Studio Templates";
            record[3] = "Incrementing tick [1] of [2]"; 
            session.Message(InstallMessage.ActionStart, record); 

            try
            {
                ProcessStartInfo startInfo = new ProcessStartInfo(
                    Path.Combine(vsPath, "devenv.exe"), "/InstallVSTemplates");
                Process process = Process.Start(startInfo);
                process.WaitForExit();

                session.Log("InstallVSTemplates: Completed successfully using path '{0}'", vsPath);
                return ActionResult.Success;
            }
            catch (Exception e)
            {
                session.Log("InstallVSTemplates: Error: " + e.Message);
                return ActionResult.Failure;
            }
        }
    }
}